package com.xyauto.theme.activity

import android.annotation.SuppressLint
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.TextView
import androidx.core.view.isInvisible
import androidx.core.view.isVisible
import com.xyauto.theme.Constant
import com.xyauto.theme.R
import com.xyauto.theme.base.BaseVMActivity
import com.xyauto.theme.fragment.BannerViewAdapter
import com.xyauto.theme.utils.AutoUtils
import com.xyauto.theme.utils.toHome
import com.xyauto.theme.view.seekbar.SeekBarEx
import com.xyauto.theme.vm.DetailViewModel
import com.zhpan.bannerview.BannerViewPager

open class DetailActivity : BaseVMActivity<DetailViewModel>() {

    companion object {
        const val DETAILS_KEY = "DetailActivity.details_key"
        const val POSITION_KEY = "DetailActivity.position_key"
        const val ACTION_APPLY_THEME = "com.xyauto.theme.activity.DetailActivity.action_apply_theme"
        const val EXTRA_KEY_NAME = "extra_key_name"
        const val EXTRA_KEY_PATH = "extra_key_path"
    }


    protected lateinit var mControlLayout: View

    protected lateinit var mApplyButton: TextView
    protected lateinit var mAppName: TextView
    protected lateinit var mSeekbarText: TextView
    protected lateinit var mSelectViw: View
    protected lateinit var mDownloadSeekbar: SeekBarEx

    protected var themeType: Int = R.string.detail_text_apply
    protected var mProgress = -1

    protected var mBannerView: BannerViewPager<String>? = null


    override fun getLayoutId() = R.layout.activity_detail

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        mApplyButton = findViewById(R.id.m_apply_button)
        mSelectViw = findViewById(R.id.m_select_viw)
        mAppName = findViewById(R.id.m_app_name)
        mSeekbarText = findViewById(R.id.m_seekbar_text)
        mDownloadSeekbar = findViewById(R.id.m_download_seekbar)
        mDownloadSeekbar.setSeekBarExEnable(false)
        mControlLayout = findViewById(R.id.m_control_layout)

        mBannerView = findViewById<BannerViewPager<String>>(R.id.m_preview_banner).also {
            it.registerLifecycleObserver(lifecycle)
            it.adapter = BannerViewAdapter()
            val bottom = AutoUtils.designHeight * (1F - 0.85F)
            it.setIndicatorMargin(0, 0, 0, AutoUtils.h(bottom.toInt()))
            it.setOnPageClickListener { _, _ ->
                mControlLayout.isVisible = !mControlLayout.isVisible
            }
            it.create()
        }
    }


    override fun initData() {
        //loadDataBean必须再订阅前面
        viewModel.loadDataBean(intent?.getParcelableExtra(DETAILS_KEY))
        viewModel.detailObserver.observe(this) {
            mSelectViw.isSelected = it.isSelected()
            mAppName.text = it.getName()
            val images = it.getPreviewImages() ?: arrayListOf(it.getPreviewImage())
            val isMultiple = images.size > 1
            mBannerView?.also {
                it.setAutoPlay(isMultiple)
                it.setCanLoop(isMultiple)
                it.setIndicatorVisibility(if (isMultiple) View.VISIBLE else View.GONE)
            }?.refreshData(images)
        }
        viewModel.typeObserver.observe(this) {
            themeType = it
            when (it) {
                R.string.detail_text_in_use -> upDateApplyButton(it, isClickable = false)
                R.string.detail_text_resume,
                R.string.detail_text_download,
                R.string.detail_text_apply -> upDateApplyButton(it)
                R.string.detail_text_downloading -> upDateApplyButton(0, true)
            }
        }
        viewModel.clearObserver.observe(this) {
            toHome()
        }
        viewModel.progressObserver?.observe(this) {
            when (it.type) {
                Constant.download_pause -> {
                    if (!TextUtils.isEmpty(it.msg)) {
                        showError(getString(R.string.detail_text_download_error_rubric), 0)
                    }
                    upDateApplyButton(R.string.detail_text_resume)
                    themeType = R.string.detail_text_resume
                }
                Constant.download_ing -> upDateApplyButton(it.progress, true)
                Constant.download_complete -> viewModel.reloadDataBean()
            }
        }
    }


    override fun initListener() {
        mApplyButton.setOnClickListener {
            when (themeType) {
                R.string.detail_text_in_use -> {}
                R.string.detail_text_resume -> viewModel.resetDownloadTask()
                R.string.detail_text_download -> viewModel.onDownload()//下载
                R.string.detail_text_apply -> viewModel.onApply(this)//应用
            }
        }
        findViewById<View>(R.id.m_back_icon).setOnClickListener { finish() }
    }

    @SuppressLint("SetTextI18n")
    private fun upDateApplyButton(msg: Int, isProcess: Boolean = false, isClickable: Boolean = true) {
        mApplyButton.isInvisible = isProcess
        mSeekbarText.isInvisible = !isProcess
        mDownloadSeekbar.isInvisible = !isProcess

        mApplyButton.isEnabled = !isProcess && isClickable

        if (isProcess) {
            if (mProgress != msg) {
                mSeekbarText.text = "$msg%"
                mDownloadSeekbar.setSeekBarProgress(msg)
            }
        } else {
            mApplyButton.setText(msg)
        }
        mProgress = msg
    }

    override fun onDestroy() {
        super.onDestroy()
        mBannerView?.removeLifecycleObserver(lifecycle)
    }
}